<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed.');
    class Expense_m extends CI_Model {
        function __construct() {
            parent::__construct();
            
        }
        
        function insert_expense($data1){
            return $this->db->insert('expenses',$data1); 
        }
        
        function get_expenses($event_id){
            $sql = "SELECT *, CONCAT(exp_cheque,'-',exp_chq_no) AS exp_chq FROM expenses WHERE event_id = ?";
            $q = $this->db->query($sql, $event_id);
            if($q->num_rows() > 0){
                return $q->result_array();
            }else{
                return FALSE;
            }
        }
        
        function get_cv_amount($event_id){
            $sql = "SELECT SUM(cv_amount) AS total FROM voucher WHERE event_id = ?";
            $q = $this->db->query($sql, $event_id);
            if($q->num_rows() > 0){
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function get_exp_actual_amount($event_id){
            $sql = "SELECT SUM(actual_amount) AS exp_total FROM expenses WHERE event_id = ?";
            $q = $this->db->query($sql, $event_id);
            if($q->num_rows() > 0){
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function get_event_initial_budget($event_id){
            $sql = "SELECT initial_budget FROM event WHERE event_id = ?";
            $q = $this->db->query($sql, $event_id);
            if($q->num_rows() > 0){
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function compute_balance($event_id){
            $sql = "SELECT (event.initial_budget-SUM(expenses.actual_amount)) AS balance FROM expenses INNER JOIN event ON expenses.event_id=event.event_id WHERE expenses.event_id = ? ";
            $q = $this->db->query($sql, $event_id);
            if($q->num_rows() > 0){
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function insert_voucher($data){
            return $this->db->insert('voucher',$data); 
        }
        
        function get_voucher($event_id){
            $sql = "SELECT *, CONCAT(cv_cheque,'-',cv_chq_no) AS cv_chq FROM voucher WHERE event_id = ?";
            $q = $this->db->query($sql, $event_id);
            if($q->num_rows() > 0){
                return $q->result_array();
            }else{
                return FALSE;
            }
        }
        
        function get_exp_entry($data){
            $this->db->where('expenses_id',$data);
            return $this->db->get('expenses');
        }
        
        function get_cv_entry($data){
            $this->db->where('cv_id',$data);
            return $this->db->get('voucher');
        }
        
        function get_table_expense($data){
            $this->db->where('event_id',$data);
            return $this->db->get('expenses');
            
        }
        
        function get_table_voucher($data){
            $this->db->where('event_id',$data);
            return $this->db->get('voucher');
        }
    }
?>
